using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace MultiLayer
{
    public class Stroke : Entity
    {
        public Stroke(Point[] pPoints, Pen pPen)
        {
            this.mPath = new GraphicsPath();
            this.mPen = pPen;

            //int left, right, top, bottom;
            //left = right = pPoints[0].X;
            //top = bottom = pPoints[0].Y;

            for (int i = 1; i < pPoints.Length; i++)
            {
                //left = Math.Min(pPoints[i].X, left);
                //right = Math.Max(pPoints[i].X, right);
                //top = Math.Min(pPoints[i].Y, top);
                //bottom = Math.Max(pPoints[i].Y, bottom);

                this.mPath.AddLine(pPoints[i - 1], pPoints[i]);
            }

            //int penWidth = (int)(this.mPen.Width / 2.0f + 0.5f);
            //this.mBoundingBox.X = left - penWidth;
            //this.mBoundingBox.Y = top - penWidth;
            //this.mBoundingBox.Width = (right - left) + penWidth * 2;
            //this.mBoundingBox.Height = (bottom - top) + penWidth * 2;
            RectangleF bounds = this.mPath.GetBounds();
            this.mBoundingBox.X = (int)(bounds.X + 0.5f);
            this.mBoundingBox.Y = (int)(bounds.Y + 0.5f);
            this.mBoundingBox.Width = (int)(bounds.Width + 0.5f);
            this.mBoundingBox.Height = (int)(bounds.Height + 0.5f);
        }

        public override bool HitTest(int pX, int pY)
        {
            return this.mPath.IsOutlineVisible(pX, pY, this.mPen);
        }

        public override void Draw(Graphics pGraphics)
        {
            pGraphics.DrawPath(this.mPen, this.mPath);
        }

        public override void Move(int pDeltaX, int pDeltaY)
        {
            Matrix mat = new Matrix();
            mat.Translate(pDeltaX, pDeltaY);
            this.mPath.Transform(mat);

            RectangleF bounds = this.mPath.GetBounds();
            this.mBoundingBox.X = (int)(bounds.X + 0.5f);
            this.mBoundingBox.Y = (int)(bounds.Y + 0.5f);
            this.mBoundingBox.Width = (int)(bounds.Width + 0.5f);
            this.mBoundingBox.Height = (int)(bounds.Height + 0.5f);
            this.mBoundingBox.Inflate((int)(this.mPen.Width / 2.0f + 0.5f), (int)(this.mPen.Width / 2.0f + 0.5f));
        }

        private GraphicsPath mPath;
        private Pen mPen;
    }
}
